﻿
//计算字符串长度
function strlen(key) {
    var l = escape(key), len;
    len = l.length - (l.length - l.replace(/\%u/g, "u").length) * 4;
    l = l.replace(/\%u/g, "uu");
    len = len - (l.length - l.replace(/\%/g, "").length) * 2;
    return len;
}

//判断字符串中是否含有特殊字符
function havespecial(key) {
    var regspecial = />|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i;
    if (regspecial.test(key)) {
        return true;
    }
    else {
        return false;
    }
}

function LTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
            i--;
        }
        s = s.substring(0, i + 1);
    }
    return s;
}
//去空格
function trim(str) {
    return RTrim(LTrim(str));
}

//是否为空
function Check_Empty(pObj) {
    var obj = eval(pObj);
    if (obj == null || trim(obj.value) == "") {
        return false;
    }
    return true;
}

//整数或小数
function IsFloat(val) {
    var s = document.getElementById(val);
    if (s.value != '' && (isNaN(s.value) || s.value < -1)) {
        return false;
    }
    return true;
}

//是否为数字
function IsInt(o) {
    var s = document.getElementById(o);
    var reg = /^[0-9]*$/;
    if (!reg.test(trim(s.value))) {
        return false;
    }
    else {
        return true;
    }
}
//验证是否为日期类型
function IsDate(sDate) {
    var aDate = sDate.replace(/-/g, "/");
    var arrDate = aDate.split("/");
    var dDate = new Date(Date.parse(aDate));
    if (!isNaN(dDate)) {
        if (dDate.getFullYear() == parseInt(arrDate[0], 10) && (dDate.getMonth() + 1) == parseInt(arrDate[1], 10) && dDate.getDate() == parseInt(arrDate[2], 10)) {
            return true;
        }
    }
    return false;
}
//邮箱地址验证
function Check_Email(strEmail) {
    /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
    var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
    if (emailReg.test(strEmail)) {
        return true;
    }
    else {
        return false;
    }
}
//验证身份证号
function IsIdCardNo(num) {
    /*
    if (isNaN(num)) {
        return false;
    }
    */
    var len = num.length, re;
    if (len == 15)
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    else if (len == 18)
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
    else {
        return false;
    }
    var a = num.match(re);
    if (a != null) {
        if (len == 15) {
            var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        } else {
            var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        if (!B) {
            //alert("输入的身份证号 " + a[0] + " 里出生日期不对！");
            return false;
        }
    }
    return true;
}
//校验手机号码：必须以数字开头，除数字外，可含有“-”
function IsMobile(s) {
    var patrn = /^[0-9]{11}$/;
    if (!patrn.exec(s)) {
        return false;
    }
    return true;
}
//验证url
function Check_Url(strUrl) {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}"
        + "|"
        + "([0-9a-z_!~*'()-]+\.)*"
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."
        + "[a-z]{2,6})"
        + "(:[0-9]{1,4})?"
        + "((/?)|"
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    if (re.test(strUrl)) {
        return true;
    }
    else {
        return false;
    }
}

//检查页面元素的自定义属性
function checkAttr(obj) {
    for (i = 0; i < obj.length; i++) {
        try {
            if (obj[i].getAttribute("need") != null && trim(obj[i].value) == "") {
                alert(obj[i].getAttribute("need"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("int") != null && trim(obj[i].value) != "" && !IsInt(obj[i].id)) {
                alert(obj[i].getAttribute("int"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("float") != null && !IsFloat(obj[i].id)) {
                alert(obj[i].getAttribute("float"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("email") != null && obj[i].value != "" && !Check_Email(obj[i].value)) {
                alert(obj[i].getAttribute("email"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("date") != null && obj[i].value != "" && !IsDate(obj[i].value)) {
                alert(obj[i].getAttribute("date"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("mobile") != null && obj[i].value != "" && !IsMobile(obj[i].value)) {
                alert(obj[i].getAttribute("mobile"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("idcardnum") != null && obj[i].value != "" && !IsIdCardNo(obj[i].value)) {
                alert(obj[i].getAttribute("idcardnum"));
                obj[i].focus();
                return false;
            }
        }
        catch (e)
        { return false; }
    }
    return true;
}
//验证字符串最大长度
function isOutside(str, len) {
    if (str.replace(/[^\x00-\xff]/g, "rr").length > len) {
        return true;
    }
    else {
        return false;
    }
}
//验证字符串长度范围
function isOutside2(str, minlen, maxlen) {
    if (str.replace(/[^\x00-\xff]/g, "rr").length >= minlen && str.replace(/[^\x00-\xff]/g, "rr").length <= maxlen) {
        return true;
    }
    else {
        return false;
    }
}
//获取对象
function oo(objId) {
    return document.getElementById(objId);
}
